<?php
class Videos_model extends Model {
	
	function Videos_model() {
	}
	  
	
	function get_total_videos(){
		$query = "SELECT  COUNT(*) as videos FROM videos ";	
		$query = $this->db->query($query);
		$row = $query->row();
		
		return  $row ? $row->videos : null;
	
	}
	
	function vote_video_up($videoId , $fbId) {
	
		$query = $this->db->query("SELECT * FROM videos_up_votes WHERE videoId = " .$videoId. " AND  fbId = " . $fbId );
		if ($query->num_rows() == 0)
		{
			$this->db->query("INSERT INTO videos_up_votes VALUES( null, '".$videoId."', '".$fbId."'  )");
		}
		
		$query = $this->db->query("SELECT COUNT(*) as votes FROM videos_up_votes WHERE videoId = " . $videoId . " GROUP BY videoId;" );
		echo $query->row()->votes;
	}
	   
	function get_videos_with_top_rated($offset){
	 
		$offset = $offset == 0 ? 1 : $offset * VIDEOS_PER_PAGE;
		//Query all the videos
		$video_query = "SELECT  C.id,  C.name, C.description,  C.video_url, COUNT(V.id) as votes
		FROM videos as C LEFT JOIN videos_up_votes as V
		ON C.id = V.videoId GROUP BY C.id  ORDER BY C.id DESC LIMIT " . VIDEOS_PER_PAGE . " OFFSET ". $offset;
		
		//Query the top rated videos
		$video_query_rated = "SELECT  C.id,  C.name, C.description,  C.video_url, COUNT(V.id) as votes
		FROM videos as C RIGHT JOIN videos_up_votes as V
		ON C.id = V.videoId GROUP BY C.id ORDER BY votes DESC LIMIT 4";
		
		$query = $this->db->query($video_query);
		$query_2 = $this->db->query($video_query_rated);
		
		$query_3 = $query->result_array();
		$count = count($query_3);
		for ($i = 0; $i < $count; $i++) 
		{
			$video = $query_3[$i]['video_url'];
			$videoId_index =  (strpos($video, "?v=") ?  strpos($video, "?v=") + 3: strpos($video, "&v=") + 3);
			$videoId = substr($video,  $videoId_index , 11) ;
			$query_3[$i]['thumb'] =   "http://i4.ytimg.com/vi/".$videoId."/default.jpg";
			$query_3[$i]['video'] =    $videoId ;
			$query_3[$i]['name_short'] =  character_limiter( $query_3[$i]['name'] , 16) ;
		}
		 
		$query_4 = $query_2->result_array();
		$count = count($query_4);
		for ($i = 0; $i < $count; $i++) 
		{
			$video = $query_4[$i]['video_url'];
			$videoId_index =  (strpos($video, "?v=") ?  strpos($video, "?v=") + 3: strpos($video, "&v=") + 3);
			$videoId = substr($video,  $videoId_index , 11) ;
			$query_4[$i]['thumb'] =   "http://i4.ytimg.com/vi/".$videoId."/default.jpg";
			$query_4[$i]['video'] =    $videoId ;
			$query_4[$i]['name_short'] =   character_limiter( $query_4[$i]['name'] , 16) ;
		}
		
		  
		$data_ar = array(
			'videos_count' => count($query_3 ),
			'videos' => $query_3 ,
			'high-rated-videos' => $query_4
		);
		
		$result = '';
		$result .= $this->parser->parse('videos_template', $data_ar, TRUE);
		return $result;
	
	}
	
	
	function get_videos($offset){
			
		$video_query = "SELECT  C.id,  C.name, C.description,  C.video_url, COUNT(V.id) as votes
		FROM videos as C LEFT JOIN videos_up_votes as V
		ON C.id = V.videoId "  .
		" GROUP BY C.id  ORDER BY C.id DESC LIMIT " . VIDEOS_PER_PAGE . " OFFSET ". $offset;
		 
		
		$query = $this->db->query($video_query); 
		
		$data_ar = array(
			'videos_count' => count($query->result_array() ),
			'videos' => $query->result_array() 
		);
		 
		$result = $this->parser->parse('videos_template', $data_ar, TRUE);
		return $result;
	
	}

}
?>